<template>
  <a-modal
    title="任务编辑"
    :width="800"
    :visible="visible"
    :confirmLoading="loading"
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }">
    <a-spin :spinning="loading">
      <a-form
        :form="form"
        v-bind="formLayout">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item
          v-if="model && model.id > 0"
          label="主键ID">
          <a-input
            v-decorator="['id', { initialValue: 0 }]"
            disabled />
        </a-form-item>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="任务名称">
              <a-input
                placeholder="请输入任务名称"
                v-decorator="['taskName', { rules: [{ required: true, message: '请填写任务名称' }] }]" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="任务目标">
              <a-input
                placeholder="请输入任务目标"
                v-decorator="['taskTarget', { rules: [{ required: true, message: '请填写任务目标' }] }]" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="指标">
              <a-input
                placeholder="请输入任务指标"
                v-decorator="['taskKpi']" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="开始时间">
              <a-date-picker
                valueFormat="YYYY-MM-DD hh:mm:ss"
                v-decorator="['beginDate', { rules: [{ required: true, message: '请填写任务名称' }] }]" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="计划交付时间">
              <a-date-picker
                valueFormat="YYYY-MM-DD hh:mm:ss"
                v-decorator="['planCompleteDate', { rules: [{ required: true, message: '请填写任务名称' }] }]" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="实际完成日期">
              <a-date-picker valueFormat="YYYY-MM-DD hh:mm:ss" v-decorator="['completeDate']" />
            </a-form-item>
          </a-col>
          <!-- <a-col :span="12">
            <a-form-item label="任务所处节点">
              <a-input v-decorator="['customer']" />
            </a-form-item>
          </a-col> -->
          <a-col :span="12">
            <a-form-item label="执行人员">
              <!-- 员工 -->
              <!-- <a-input placeholder="请选择执行人员" v-decorator="['staffId', { rules: [{ required: true, message: '请填写任务名称' }] }]" /> -->
              <a-select
                :mode="taskLevel1 ? 'multiple' : 'default' "
                allowClear
                show-search
                :filter-option="filterOption"
                placeholder="请选择执行人员"
                v-decorator="['userIds',{ rules: [{ required: true, message: '请选择执行人员' }] }]">
                <a-select-option
                  v-for="i in users"
                  :key="i.userId"
                  :values="i.userId">
                  {{ i.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="任务类型">
              <a-select
                :disabled="true"
                v-decorator="['taskType', { rules: [{ required: true, message: '请填写任务名称' }] }]">
                <a-select-option :value="1">项目任务</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <!-- <a-col :span="12">
            <a-form-item label="状态">
              <a-select placeholder="请选择状态" v-decorator="['status', { rules: [{ required: true, message: '请输入任务状态' }] }]" >
                <a-select-option :value="1">新建</a-select-option>
                <a-select-option :value="2">进行中</a-select-option>
                <a-select-option :value="3">完成中</a-select-option>
              </a-select>
            </a-form-item>
          </a-col> -->
          <a-col
            :span="12"
            v-if="taskLevel2">
            <a-form-item label="绩效分">
              <a-input
                placeholder="请选输入绩效分"
                v-decorator="['performanceCategory']" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="等级">
              <a-select
                placeholder="请选择等级"
                v-decorator="['level', { rules: [{ required: true, message: '请输入任务等级' }] }]">
                <a-select-option :value="1">紧急</a-select-option>
                <a-select-option :value="2">加急</a-select-option>
                <a-select-option :value="3">普通</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col
            :span="12"
            v-if="taskLevel2">
            <a-form-item label="任务进度">
              <a-select
                placeholder="请选择任务进度"
                v-decorator="['progress']">
                <a-select-option :value="1">等待中</a-select-option>
                <a-select-option :value="2">进行中</a-select-option>
                <a-select-option :value="3">完成</a-select-option>
                <a-select-option :value="4">暂停</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col
            :span="12"
            v-if="taskLevel1">
            <a-form-item label="一级任务权重">
              <a-input
                placeholder="请输入一级任务分"
                v-decorator="['proportion', { rules: [{ required: true, message: '请输入1级任务权重' }] }]" />
            </a-form-item>
          </a-col>
          <a-col
            :span="12"
            v-if="taskLevel3">
            <a-form-item label="三级任务状态">
              <a-select
                placeholder="请输入三级任务完成状态"
                v-decorator="['status', { rules: [{ required: true, message: '请输入3级任务状态' }] }]">
                <a-select-option :value="1">等待中</a-select-option>
                <a-select-option :value="2">进行中</a-select-option>
                <a-select-option :value="3">完成</a-select-option>
                <a-select-option :value="4">暂停</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="完成质量">
              <a-select
                placeholder="请选择完成质量"
                v-decorator="['completeQuality']">
                <a-select-option :value="1">合格</a-select-option>
                <a-select-option :value="2">不合格</a-select-option>
                <a-select-option :value="3">良</a-select-option>
                <a-select-option :value="4">优</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <!-- <a-col :span="12">
            <a-form-item label="任务进度">
              <a-input placeholder="请选择执行人员" v-decorator="['projectName', { rules: [{ required: true, message: '请填写任务名称' }] }]" />
            </a-form-item>
          </a-col> -->
          <a-col :span="12">
            <a-form-item label="备注">
              <a-input
                placeholder="请输入备注"
                v-decorator="['note']" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
// import {
//   roleSelect
// } from '@/api/manage'
// 表单字段
const fields = ['id', 'createDate', 'projectName', 'projectType', 'completeDate', 'customer', 'staff']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    users: {
      type: Array,
      required: true
    },
    model: {
      type: Object,
      default: () => null
    },
    taskLevel1: {
      type: Boolean,
      default: () => false
    },
    taskLevel2: {
      type: Boolean,
      default: () => false
    },
    taskLevel3: {
      type: Boolean,
      default: () => false
    }
  },
  mounted () {
      // roleSelect().then((res) => { this.users = res.data })
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      form: this.$form.createForm(this),
      defaultFileList: []
      // users: []
    }
  },
  created () {
    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  methods: {
    filterOption (input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    }
  }
}
</script>
